<?php
/**
 * Created by PhpStorm.
 * User: lxq
 * Date: 2018/4/16
 * Time: 9:25
 */
class foodControl extends  BaseHomeControl {

    public function __construct()
    {
        if($_GET['op']=='place_info'){
            $this->_must_login = true;
        }
        parent::__construct();
        Tpl::setLayout('new_layout');
        Tpl::setDir('food');
    }

    /**
     * 美食列表页
     */
    public function indexOp(){
        /* @var foodModel $food_model*/
        $food_model = Model('food');
        $condition = array();
        $condition['fd_status'] = 1;
        if($_SESSION['location']){
            $location = $_SESSION['location'];
            $field = '*';
            //$distance = 'SQRT(pow('. $location['lat'] .' - hotel_lat, 2) + pow('. $location['lng'] .'  - hotel_lng, 2)) AS distance';
            $distance = 'SQRT(pow('. $location['lng'] .' - food_lat, 2) + pow('. $location['lat'] .'  - food_lng, 2)) AS distance';
            $field .= ','.$distance;
        }
        if(isset($_GET['type'])){
            $food_type = intval($_GET['type'])==1?1:0;
            $condition['fd_type'] = $food_type;
        }
        if($_SESSION['location']){
            $order = 'distance asc';
        }else{
            $order = 'price asc';
        }
        $foods_list = $food_model->getFoodListPage($condition, $field, 10,$order);
        if($foods_list && is_array($foods_list)){

            foreach($foods_list as $key =>$item){
                //get_thumb($item['fd_thumb'],FOOD_MARK,240);
                //$foods_list[$key]['fd_thumb'] = getGoodsImg($item['fd_thumb'],FOOD_MARK);
                $foods_list[$key]['fd_thumb'] =  get_thumb($item['fd_thumb'],FOOD_MARK,240);

                if($item['distance']){
                    $distance = $item['distance'];
                    $distance = round($distance * 10000) / 100;
                    if($distance<0.1){
                        $foods_list[$key]['distance']='小于100m';
                    }else{
                        $foods_list[$key]['distance'] = $distance."km"; // KM
                    }
                }
                if($item['fd_type']==0 && $item['open_spec']==1){
                    $spec_info = Model('spec')->getSpecInfo_v2(array('goods_id'=>$item['fd_id'],'is_show'=>1,'type'=>3));
                    $foods_list[$key]['price'] = $spec_info['price'];
                    $foods_list[$key]['stock'] = $spec_info['stock'];
                }
            }
        }
        Tpl::output('food_list',$foods_list);
        //AJAX加载分页
        if(isset($_GET['isajax']) && $_GET['isajax'] == 1){
            Tpl::showpage('food.index.page','null_layout');
            exit;
        }
        Tpl::output('TotalNum',pagecmd("gettotalnum"));
        Tpl::output('TotalPage',pagecmd("gettotalpage"));
        Tpl::showpage('food.index');
    }

    /**
     * 美食搜索页
     */
    public function searchOp()
    {
        /* @var foodModel $food_model*/
        $food_model = Model('food');
        /** @var member_merchantModel $merchant_model */
        $merchant_model = Model('member_merchant');
        $category_model = Model('ticket_category');
        $condition = array();
        /*$condition['fd_type'] = 0;//固定美食*/
        $condition['fd_status'] = 1;
        //查询条件
        if($cate_id = $_GET['cate_id']){
            $condition['cate_id'] = intval($cate_id);//分类ID
        }
        if($area_id = $_GET['area']){
            $condition['fd_area_id'] = $area_id; //地区ID
        }
        if($keyword = trim($_GET['keyword'])){
            $condition['fd_title'] = array('like','%'.$keyword.'%');
        }
        //计算距离
        if($_SESSION['location']){
            $location = $_SESSION['location'];
            $field = '*';
            //$distance = 'SQRT(pow('. $location['lat'] .' - hotel_lat, 2) + pow('. $location['lng'] .'  - hotel_lng, 2)) AS distance';
            $distance = 'SQRT(pow('. $location['lng'] .' - food_lat, 2) + pow('. $location['lat'] .'  - food_lng, 2)) AS distance';
            $field .= ','.$distance;
        }
        if($order=$_GET['order']){
            //排序
            $order_arr = explode('|',$order);
            $order = $order_arr[0].' '.$order_arr[1];
        }else{
            if($_SESSION['location']){
                $order = 'distance asc';
            }
        }
        $list = $food_model->getFoodListPage($condition,$field,5,$order);
        if($list && is_array($list)){
            foreach($list as $k => $v){
               // $list[$k]['fd_thumb'] = getGoodsImg($v['fd_thumb'],FOOD_MARK);
                $list[$k]['fd_thumb'] = get_thumb($v['fd_thumb'],FOOD_MARK,240);
                if($v['distance']){
                    $distance = $v['distance'];
                    $distance = round($distance * 10000) / 100;
                    if($distance<0.1){
                        $list[$k]['distance']='小于100m';
                    }else{
                        $list[$k]['distance'] = $distance."km"; // KM
                    }
                }
                if($v['fd_type']==0 && $v['open_spec']==1){
                    $spec_info = Model('spec')->getSpecInfo_v2(array('goods_id'=>$v['fd_id'],'is_show'=>1,'type'=>3));
                    $list[$k]['price'] = $spec_info['price'];
                    $list[$k]['stock'] = $spec_info['stock'];
                }
            }
        }
        Tpl::output('food_list',$list);
        //AJAX加载分页
        if(isset($_GET['isajax']) && $_GET['isajax'] == 1){
            Tpl::showpage('food.search.page','null_layout');
            exit;
        }
        $city = $food_model->getFoodAreaId();
        if($city){
            $area_arr = array();
            $area_model  = Model('area');
            foreach($city as $item =>$value){
                $city[$item]['name'] = $area_model->getfby_id($value['fd_area_id']);
            }
            Tpl::output('area_arr',$city);
        }
        //美食底下所有分类
        $cate = $category_model->getCategoryList(array('tc_pid'=>FOOD_MARK,'is_del'=>0));
        Tpl::output('TotalNum',pagecmd("gettotalnum"));
        Tpl::output('TotalPage',pagecmd("gettotalpage"));
        Tpl::output('cate',$cate);
        Tpl::showpage('food.search');

    }

    /**
     * 美食详情页面
     */
    public function infoOp()
    {
        $f_id = intval($_GET['fid']);
        if($f_id>0){
            /* @var foodModel $food_model*/
            $food_model = Model('food');
            /* @var ticket_imagesModel  $images_model */
            $images_model = Model('ticket_images');
            /* @var member_merchantModel  $merchant_model */
            $merchant_model = Model('member_merchant');
            $condition = array();
            $condition['fd_status'] = 1;
            $condition['fd_id'] = $f_id;
            $info = $food_model->getFoodInfo($condition);
            if(empty($info)){
                showMessage('您来晚了,商品已下架！');
            }
            $images = $images_model->getTicketImageList(array(
                'tk_id'=>$f_id,
                'img_deleted'=>0,
                'type'=>FOOD_MARK
            ));//获取商品相册
            if($images && is_array($images)){
                foreach($images as $image =>$value){
                    //$images[$image]['img_path'] = getGoodsImg($value['img_path'],FOOD_MARK);
                    $images[$image]['img_path'] = get_thumb($value['img_path'],FOOD_MARK,360);
                    //$list[$k]['fd_thumb'] = get_thumb($v['fd_thumb'],FOOD_MARK,240);
                }
            }
            //获取商家信息
            $merchant_info = $merchant_model->getMerchantInfo(array('merchant_id'=>$info['merchant_id']));
            $merchant_info['mc_cover_img'] = UPLOAD.'product/'.$merchant_info['mc_cover_img'];
            //获取美食商家底下的评论条数
            /* @var commentModel  $comment_model*/
            $comment_model = Model('comment');
            $cot_count = $comment_model->getCommentCount(array('merchant_id'=>$info['merchant_id'],'cmt_type'=>FOOD_MARK));
            if($cot_count>0){
                //获取两条评论
                $cmt_list = $comment_model->getCommentPage(array('merchant_id'=>$info['merchant_id'],'cmt_type'=>FOOD_MARK),'*',2);
                if($cmt_list && is_array($cmt_list)){
                    foreach($cmt_list as $key =>$value){
                       if($value['pic_1']){
                           $cmt_list[$key]['pic_1'] = UPLOAD.'comment/'.$value['pic_1'];
                       }
                        if($value['pic_2']){
                            $cmt_list[$key]['pic_2'] = UPLOAD.'comment/'.$value['pic_2'];
                        }
                        if($value['pic_3']){
                            $cmt_list[$key]['pic_3'] = UPLOAD.'comment/'.$value['pic_3'];
                        }
                        $cmt_list[$key]['member_name'] = nickname_hide($value['member_name']);
                    }
                    Tpl::output('cmt_list',$cmt_list);
                }
            }
            Tpl::output('cot_count',$cot_count);
            Tpl::output('merchant_info',$merchant_info);
            Tpl::output('images',$images);

            //规格
            if($info['fd_type']==0 && $info['open_spec']==1){
                $spec_list = Model('spec')->getSpecList(array('goods_id'=>$info['fd_id'],'is_show'=>1,'type'=>3),'*',"spec_sort asc");
                Tpl::output('spec_list',$spec_list);
                $info['price'] = $spec_list[0]['price'];
                $info['fd_market_price'] = $spec_list[0]['market_price'];
                $info['sales'] = empty($spec_list[0]['sales'])?'0':$spec_list[0]['sales'];
                $info['spec_id'] = $spec_list[0]['spec_id'];
            }

            Tpl::output('food_info',$info);
            if($info['fd_type']==1){
                //代金券
                Tpl::showpage('food.ticket.info');
            }else{
                Tpl::showpage('food.info');
            }

        }
    }


    /**
     * 规格详情
     */
    public function get_spec_infoOp(){
        $spec_id = getgpc("spec_id");
        $spec_model = Model('spec');
        $info = $spec_model->getSpecInfo(array('spec_id'=>$spec_id));
        if(empty($info)){
            die(json_encode(array('tab' => 'error', 'info' => '参数错误')));
        }else{
            if($info['is_show']==0){
                die(json_encode(array('tab' => 'error', 'info' => '商品已下架！')));
            }
            die(json_encode(array('tab' => 'success', 'info' => '成功','data'=>$info)));
        }
    }

    /**
     * 店家的所有商品
     */
    public function merchant_foodOp()
    {
        $mc_id = intval($_GET['mcid']);
        if($mc_id>0){
            /* @var member_merchantModel  $merchant_model */
            $merchant_model = Model('member_merchant');
            $merchant_info = $merchant_model->getMerchantInfo(array('merchant_id'=>$mc_id));
            if($merchant_info['merchant_id']!=$mc_id){
                showMessage('商户不存在！');
            }
            $merchant_info['mc_cover_img'] = UPLOAD.'product/'.$merchant_info['mc_cover_img'];
            /* @var foodModel $food_model*/
            $food_model = Model('food');
            //获取商品
            $condition = array();
            $condition['merchant_id'] = $mc_id;
            $condition['fd_status'] = 1;
            $condition['fd_type'] = 1;//1为抵扣券
            $ticket_list = $food_model->getFoodList($condition);
            //抵扣券
            if($ticket_list && is_array($ticket_list)){
                foreach($ticket_list as $key =>$item){
                    //$ticket_list[$key]['fd_thumb'] = getGoodsImg($item['fd_thumb'],FOOD_MARK);
                    $ticket_list[$key]['fd_thumb'] = get_thumb($item['fd_thumb'],FOOD_MARK,240);
                }
            }
            Tpl::output('ticket_list',$ticket_list);
            $condition['fd_type'] = 0;
            //套餐列表
            $food_list = $food_model->getFoodList($condition);
            if($food_list && is_array($food_list)){
                foreach($food_list as $key =>$item){
                    //$food_list[$key]['fd_thumb'] = getGoodsImg($item['fd_thumb'],FOOD_MARK);
                    $food_list[$key]['fd_thumb'] = get_thumb($item['fd_thumb'],FOOD_MARK,1280);
                    if($item['fd_type']==0 && $item['open_spec']==1){
                        $spec_info = Model('spec')->getSpecInfo_v2(array('goods_id'=>$item['fd_id'],'is_show'=>1,'type'=>3));
                        $food_list[$key]['price'] = $spec_info['price'];
                        $food_list[$key]['fd_market_price'] = $spec_info['market_price'];
                        $food_list[$key]['stock'] = $spec_info['stock'];
                    }
                }
            }

            Tpl::output('food_list',$food_list);
            //AJAX加载分页
            if(isset($_GET['isajax']) && $_GET['isajax'] == 1){
                Tpl::showpage('hotel.index.page','null_layout');
                exit;
            }
            Tpl::output('merchant_info',$merchant_info);
            Tpl::output('TotalNum',pagecmd("gettotalnum"));
            Tpl::output('TotalPage',pagecmd("gettotalpage"));
            Tpl::showpage('merchant.food');
        }
    }

    /**
     * 确认订单页
     */
    public function place_infoOp(){
        $fd_id = intval($_GET['fd_id']);
        if($fd_id>0){
            /* @var foodModel $food_model*/
            $food_model = Model('food');
            $food_info = $food_model->getFoodInfo(array('fd_id'=>$fd_id,'fd_status'=>1));
            if(empty($food_info)){
                showMessage('商品不存在');
            }
            $spec_id = getgpc("spec_id");
            if(!empty($spec_id)){
                $spec_model = Model('spec');
                $spec_info = $spec_model->getSpecInfo(array('spec_id'=>$spec_id));

                if(!empty($spec_info)){
                    $food_info['price'] = $spec_info['price'];
                    Tpl::output('spec_info',$spec_info);
                    $food_info['spec_name'] = $spec_info['spec_name'];
                }
            }
           // $food_info['fd_thumb'] = getGoodsImg($food_info['fd_thumb'],FOOD_MARK);
            $food_info['fd_thumb'] = get_thumb($food_info['fd_thumb'],FOOD_MARK,240);
            Tpl::output('food_info',$food_info);
            Tpl::showpage('place_info');
        }
    }
}